﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Jerry.DroidBuilder.Common;
using Jerry.DroidBuilder.Controls;
using Jerry.DroidBuilder.Extensibility;

namespace Jerry.DroidBuilder
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public DroidBuilderViewModel ViewModel
        {
            get { return (DroidBuilderViewModel)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }

        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.Register("ViewModel", typeof(DroidBuilderViewModel), typeof(MainWindow), new PropertyMetadata(new DroidBuilderViewModel()));

        private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!IsVisible)
            {
                return;
            }

            ViewModel.Initialize();

            var plugins = PluginManager.LoadPlugins();

            foreach (var plugin in plugins)
            {
                plugin.Initialize();
            }
        }

        private void OnContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            var item = Util.FindParent<TreeListViewItem>(e.OriginalSource as DependencyObject);

            if (null != item && item.DataContext is RomItem)
            {
                item.IsSelected = true;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void OnLinkClick(object sender, RoutedEventArgs e)
        {
            Process.Start((sender as Hyperlink).NavigateUri.AbsoluteUri);
        }
    }
}
